var e = u(require("../../../@babel/runtime/regenerator")),
  t = require("../../../@babel/runtime/helpers/toConsumableArray"),
  a = require("../../../@babel/runtime/helpers/asyncToGenerator"),
  n = u(require("../../../config/index")),
  i = require("@/services/checkList"),
  r = require("../../../utils/index"),
  c = require("@/services/base/api"),
  s = require("@/services/user-service"),
  o = require("../../../utils/canvas"),
  d = u(require("dayjs"));
function u(e) {
  return e && e.__esModule ? e : { default: e };
}
Page({
  data: {
    CDN: n.default.CDN,
    title: "我的打卡",
    position: !1,
    background: "#FAF3E8",
    iconTheme: "black",
    titleColor: "#000",
    currentIndex: 0,
    isChecked: !1,
    ctx: "",
    canvas: "",
    dpr: 1,
    param: {},
    checkList: [],
    loading: !1,
    name: "",
    headImg: "",
    mainstay_id: "",
    poi_id: "",
  },
  onShow: function () {
    var n = this;
    return a(
      e.default.mark(function a() {
        var o, u, h, f;
        return e.default.wrap(function (e) {
          for (;;)
            switch ((e.prev = e.next)) {
              case 0:
                return (
                  n.checkLogin(),
                  wx
                    .createSelectorQuery()
                    .select("#canvas")
                    .fields({ node: !0, size: !0 })
                    .exec(function (e) {
                      var t = e[0].node,
                        a = t.getContext("2d"),
                        i = wx.getSystemInfoSync().pixelRatio;
                      (t.width = e[0].width * i),
                        (t.height = e[0].height * i),
                        a.scale(i, i),
                        n.setData({ ctx: a, dpr: i, canvas: t });
                    }),
                  (e.next = 5),
                  (0, s.getUserId)()
                );
              case 5:
                return (
                  (u = e.sent),
                  (e.next = 8),
                  (0, c.getCheckList)({ open_id: u })
                );
              case 8:
                null != (h = e.sent) &&
                  null !== (o = h.data) &&
                  void 0 !== o &&
                  o.list.length &&
                  h.data.list.forEach(function (e) {
                    if (e.no) {
                      var t = i.checkList.findIndex(function (t) {
                        return t.mainstay_id === e.exhibit_id;
                      });
                      -1 !== t &&
                        ((i.checkList[t].no = n.order(e.no)),
                        (i.checkList[t].isChecked = !0),
                        (i.checkList[t].time = (0, d.default)(
                          e.create_time,
                        ).format("YYYY年MM月DD日")));
                    }
                  }),
                  (f = [].concat(
                    t(
                      (0, r.deepClone)(i.checkList).filter(function (e) {
                        return e.isChecked;
                      }),
                    ),
                    t(
                      (0, r.deepClone)(i.checkList).filter(function (e) {
                        return !e.isChecked;
                      }),
                    ),
                  )),
                  n.setData({
                    currentIndex: 0,
                    checkList: f,
                    isChecked: f[0].isChecked,
                    mainstay_id: f[0].mainstay_id,
                  }),
                  n.getPoiInfo(n.data.mainstay_id);
              case 13:
              case "end":
                return e.stop();
            }
        }, a);
      }),
    )();
  },
  getPoiInfo: function (t) {
    var n = this;
    return a(
      e.default.mark(function a() {
        var i, r;
        return e.default.wrap(function (e) {
          for (;;)
            switch ((e.prev = e.next)) {
              case 0:
                return (
                  (e.next = 2), (0, c.postExhibitInfo)({ exhibit_id: [t] })
                );
              case 2:
                (r = e.sent),
                  n.setData({
                    poi_id:
                      null != r &&
                      null !== (i = r.data[0]) &&
                      void 0 !== i &&
                      i.poi_id
                        ? r.data[0].poi_id
                        : "",
                  });
              case 4:
              case "end":
                return e.stop();
            }
        }, a);
      }),
    )();
  },
  checkLogin: function () {
    if (wx.getStorageSync("san_main_user_info")) {
      var e = wx.getStorageSync("san_main_user_info");
      this.setData({ name: e.nick, headImg: e.avatar });
    }
  },
  order: function (e) {
    if (e)
      for (var t = String(e).length, a = 0; a < 7 - t; a++) e = "0".concat(e);
    return "".concat(e);
  },
  swiperchange: function (e) {
    var t = e.detail.current;
    this.setData({
      currentIndex: t,
      isChecked: this.data.checkList[t].isChecked,
      mainstay_id: this.data.checkList[t].mainstay_id,
    }),
      this.getPoiInfo(this.data.mainstay_id);
  },
  save: function () {
    var t = this;
    return a(
      e.default.mark(function a() {
        var n, i, r, c, s, d, u, h, f, l;
        return e.default.wrap(function (e) {
          for (;;)
            switch ((e.prev = e.next)) {
              case 0:
                return (
                  t.setData({ loading: !0 }),
                  (n = t.data),
                  (i = n.checkList),
                  (r = n.currentIndex),
                  (c = n.ctx),
                  (s = n.canvas),
                  (d = n.dpr),
                  (u = n.CDN),
                  (h = n.name),
                  (f = n.headImg),
                  (l = t.data.param),
                  ((l = i[r]).src = i[r].img),
                  (l.shadowSrc = "/check/shadow/1686198884764_Z3jej8mA.png"),
                  t.setData({ param: l }),
                  (e.next = 9),
                  (0, o.drawImage)(l, {
                    ctx: c,
                    canvas: s,
                    dpr: wx.getSystemInfoSync().model.includes("iPad")
                      ? 3.7
                      : d,
                    CDN: u,
                    name: h,
                    headImg: f,
                  })
                );
              case 9:
                setTimeout(function () {
                  t.setData({ loading: !1 });
                }, 1e3);
              case 10:
              case "end":
                return e.stop();
            }
        }, a);
      }),
    )();
  },
  check: function () {
    this.data.poi_id;
    wx.showToast({ title: "暂未上线，敬请期待", icon: "none", duration: 2500 });
  },
});
